<template>
  <div>
  <!-- flex容器 -->
   <el-container class="my-layout">
    <!-- 侧边栏 -->
     <el-aside width="200px">
      <img width="100%" src="@/assets/images/logo.png" alt="">
      <el-menu
        background-color="#023"
        style="border-right:none"
        text-color="#fff"
        :default-active="$route.path"
        router
        class="el-menu-vertical-demo">
  <!-- default-active="2"表示默认选中的菜单项index=1 -->
        <el-menu-item index="/">
          <i class="el-icon-s-home"></i>
            <span>数据概览</span>
        </el-menu-item>
        <el-menu-item index="/article" >
          <i class="el-icon-document"></i>
           <span slot="title">内容管理</span>
        </el-menu-item>
        <el-menu-item index="/publish">
          <i class="el-icon-s-promotion"></i>
          <span>发布文章</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
     <el-container>
      <!-- 头部 -->
       <el-header>
       <span>{{user.name || user.mobile}}</span>
       <el-link @click="open" icon="el-icon-unlock" :underline="false">退出</el-link>
       </el-header>
      <!-- 主体 -->
       <el-main>
        <router-view></router-view>
       </el-main>
     </el-container>
   </el-container>
  </div>
  </template>
<script>
import auth from '@/utils/token'
export default {
  name: 'LayoutView',
  data () {
    return {
      user: {}
    }
  },
  methods: {
    open () {
      this.$confirm('此操作将退出登录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 1. 清空token
        auth.delToken()
        // 2. 跳转到登录页
        this.$router.push('/login')
        // 3. 提示
        this.$message.success('退出成功')
      }).catch(() => {
      })
    }
  },
  async created () {
    // 拦截器中自动拼接token
    const res = await this.$http.get('/v1_0/user/profile')
    this.user = res.data.data
  }
}
</script>
  <style scoped>
  .my-layout {
    /* 1vh:视口高度的1% 1vw:视口宽度的1% */
    height: 100vh;
  }
  .el-aside {
    background-color: #023;
  }
  .el-header {
    border-bottom: 1px solid #ddd;
    display: flex;
    /* 主轴 */
    justify-content: end;
    /* 交叉轴 */
    align-items: center;
    /* 每个元素间距 */
    gap: 10px;
  }

  </style>
